<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RecommendationService
 *
 * @author Administrator
 */
class RecommendationService {

    function create() {
        return D('recommendation')->create();
    }

    function insert($r) {
        return D('recommendation')->add($r);
    }

    function update($r) {
        return D('recommendation')->save($r);
    }

    function delete($id) {
        $where['id'] = array('eq', $id);

        return D('recommendation')->where($where)->delete();
    }

    function findRById($id) {
        $where['id'] = array('eq', $id);
        return D('recommendation')->where($where)->find();
    }

    function countByUId($uId) {
        $where['user_id'] = array('eq', $uId);

        return D('recommendation')->where($where)->count();
    }

    function listByUId($uId, $limit, $order = 'datetime desc') {
        $where['user_id'] = array('eq', $uId);

        return D('recommendation')->where($where)->limit($limit)->order($order)->select();
    }

    function countAll($add, $reply) {
        $where;
        if (!is_null($add)) {
            $where['is_add'] = array('eq', $add);
        }
        if (1 == $reply) {
            $where['reply'] = array('exp', 'is not null');
        } elseif (0 == $reply && !is_null($reply)) {
            $where['reply'] = array('exp', 'is null');
        }

        return D('recommendation')->where($where)->count();
    }

    function listAll($limit, $add, $reply, $order='datetime desc') {
        $where;
        if (!is_null($add)) {
            $where['is_add'] = array('eq', $add);
        }
        if (1 == $reply) {
            $where['reply'] = array('exp', 'is not null');
        } elseif (0 == $reply && !is_null($reply)) {
            $where['reply'] = array('exp', 'is null');
        }

        return D('recommendation')->where($where)->limit($limit)->order($order)->select();
    }

}

?>
